package com.hardware.view.service;

import com.hardware.dll.AidierDLL;
import com.hardware.enums.AidierStatus;
import com.hardware.utils.MyDateUtil;

import java.io.UnsupportedEncodingException;

/**
 * 测试窗口 事件实现
 *
 * @作者 韩宗森
 * @创建时间 2024年11月01日 10:14
 */
public class TestService {

    /**
     * 打开串口
     */
    public int openPort() {
        System.out.println("TestViewService...  ");
        // 先关闭
        closePort();
        //
        Integer softwareIn = 18;
        String serverIn="10.147.18.167";
        String usernameIn="user";
        Integer portIn=1;
        Integer encoderIn=0;
        Integer TmEncoderIn=1;
        try {
            int software = softwareIn;
            byte[] server = serverIn.getBytes("GBK");
            byte[] username = usernameIn.getBytes("GBK");
            int port = portIn;
            int Encoder = encoderIn;
            int TMEncoder = TmEncoderIn;
            // 开始 初始化
            int ret = AidierDLL.INSTANCE.Init(software, server, username, port, Encoder, TMEncoder);

            AidierStatus aidierStatus = AidierStatus.fromKey(ret);

         String   msg = "状态码: " + aidierStatus.getKey() + ", 说明: " + aidierStatus.getValue();
            System.out.println("接口初始化: " + msg);
            return ret;
        } catch (UnsupportedEncodingException e) {
            System.out.println("String转byte 有异常...");
            e.printStackTrace();
        }

        return -1;
    }

    /**
     * Beep(蜂鸣器)
     */
    public int beep() {
        // 声音和LED状态。11-绿灯1秒，长叫一声；12-红灯1秒，长叫一声；15-红灯1秒，短叫两声；16-短叫一声。
        int ret = AidierDLL.INSTANCE.Reader_Beep(11);
        AidierStatus aidierStatus = AidierStatus.fromKey(ret);
        System.out.println(MyDateUtil.currentTime() + "beep Reader_Beep():状态码: " + aidierStatus.getKey() + ", 说明: " + aidierStatus.getValue());
        return ret;
    }

    /**
     * 关闭串口
     */
    public int closePort() {
        int ret = AidierDLL.INSTANCE.EndSession();
        AidierStatus aidierStatus = AidierStatus.fromKey(ret);
        System.out.println(MyDateUtil.currentTime() + "关闭串口 EndSession():状态码: " + aidierStatus.getKey() + ", 说明: " + aidierStatus.getValue());
        return ret;
    }

    /**
     * 写卡
     *
     * @param roomNumText 房号
     * @param stimeText   起止时间
     * @param isCopy      是否复制
     */
    public int writeCard(String roomNumText, String stimeText, boolean isCopy) {
        try {
            // 先转字符
            byte[] room = roomNumText.getBytes("GBK"); // 6字节字符串，必须是门锁系统设置的房号
            byte[] gate = "00".getBytes("GBK"); // 公共通道，字符串参数，“00”表示按默认授权通道，“99”表示授权所有公共通道，其他为指定通道代码。例如：“010203”表示授权01、02、03三个通道。
            byte[] stime = stimeText.getBytes("GBK");

            int ret = AidierDLL.INSTANCE.NewKey(room, gate, stime, null, null, 1, 0, null, null);
            AidierStatus aidierStatus = AidierStatus.fromKey(ret);
            System.out.println(MyDateUtil.currentTime() + "写卡 NewKey():状态码: " + aidierStatus.getKey() + ", 说明: " + aidierStatus.getValue());

            // 复制卡
            if (isCopy) {
                copyCard(room, gate, stime);
            }
            return ret;
        } catch (UnsupportedEncodingException e) {
            System.out.println("String转byte 有异常...");
            e.printStackTrace();
        }
        return -1;
    }

    /**
     * 复制卡
     */
    public int copyCard(byte[] room, byte[] gate, byte[] stime) {

        // 复制卡
        int ret = AidierDLL.INSTANCE.DupKey(room, gate, stime, null, null, 1, 0, null, null);
        AidierStatus aidierStatus = AidierStatus.fromKey(ret);
        System.out.println(MyDateUtil.currentTime() + "复制卡 DupKey():状态码: " + aidierStatus.getKey() + ", 说明: " + aidierStatus.getValue());
        return ret;
    }

    /**
     * 读卡
     */
    public int readCard() {
        byte[] room = new byte[31]; // 接收返回的房号，建议10字节。
        byte[] gate = new byte[31]; // 接收返回的授权公共通道
        byte[] stime = new byte[31]; // 起止时间，24字节字符串
        byte[] guestname = new byte[31]; // 接收返回的客人姓名
        byte[] guestid = new byte[31]; // 接收返回的客人ID
        long cardno = 0; // 接收返回的卡号
        int st = 0; // 整形指针，接收返回的卡状态，1-正常使用，3-正常注销，4-遗失注销，5-损毁注销，6-自动注销。可以为NULL。
        // 读卡
        int ret = AidierDLL.INSTANCE.ReadCard(room, gate, stime, guestname, guestid, null, null, cardno, st);
        AidierStatus aidierStatus = AidierStatus.fromKey(ret);
        System.out.println(MyDateUtil.currentTime() + "读卡 DupKey():状态码: " + aidierStatus.getKey() + ", 说明: " + aidierStatus.getValue());

        try {
            String roomOut = new String(room, "GBK").trim();
            String gateOut = new String(gate, "GBK").trim();
            String stimeOut = new String(stime, "GBK").trim();
            String guestnameOut = new String(guestname, "GBK").trim();
            String guestidOut = new String(guestid, "GBK").trim();

            System.out.println(MyDateUtil.currentTime()
                    + "roomOut: " + roomOut
                    + ", gateOut: " + gateOut
                    + ", stimeOut: " + stimeOut
                    + ", guestnameOut: " + guestnameOut
                    + ", guestidOut: " + guestidOut
                    + ", cardnoOut: " + cardno
                    + ", st: " + st
            );

        } catch (UnsupportedEncodingException e) {
            System.out.println("读卡: byte[] 转 String 异常...");
            e.printStackTrace();
        }
        return ret;
    }

    /**
     * 销卡
     */
    public int destroyCard(long cardno) {
        // 销卡
        int ret = AidierDLL.INSTANCE.EraseCard(cardno, null, null);
        AidierStatus aidierStatus = AidierStatus.fromKey(ret);
        System.out.println(MyDateUtil.currentTime() + "销卡 EraseCard():状态码: " + aidierStatus.getKey() + ", 说明: " + aidierStatus.getValue());
        return ret;
    }
}
